package io.renren.common.utils;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.LinkedList;

public class IntDexUtils {

    /**
     * 十进制转十六进制方法
     *
     * @param x 待转换的十进制数
     */
    public static String DtoH(BigInteger x) {
        // 存放十六进制的十六个数
        String[] str = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
        // 临时存放所有余数
        LinkedList<BigInteger> linkedList = new LinkedList<BigInteger>();
        // m:临时存放每次计算的余数
        BigInteger m = new BigInteger("0");
        while (x.compareTo(new BigInteger("0")) > 0) {
//            m = x % 16;// m存余数
            BigInteger[] resBigIntegers = x.divideAndRemainder(new BigInteger("16"));
//            System.out.println("两数相除，整除结果为：" + resBigIntegers[0]  +
//                    ",余数为：" + resBigIntegers[1]);
            m = resBigIntegers[1];// m存余数
            linkedList.push(m);
//            x = x / 16;
            x = resBigIntegers[0];
        }
        // reuslt：存放结果
        String reuslt = "";
        while (linkedList.size() != 0) {
            BigInteger popInteger = linkedList.pop();
            reuslt = reuslt + str[popInteger.intValue()];
        }
        String newValue = "";
        for (int i = 0; i < reuslt.length(); i++) {
            if (i > 0 && i % 2 == 0) {
//                System.out.println(i);
                newValue = newValue + "-";
//                System.out.println(newValue);
            }
            newValue = newValue + reuslt.substring(i, i + 1);
        }
        return newValue;
    }

    /**
     * 十六进制转十进制
     *
     * @param str 待转换的十六进制数
     */
    public static int HtoD(String str) {
        //去掉-
        str = str.replace("-", "");
        // map:搭建十六进制中A-F对应的数值
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("A", 10);
        map.put("B", 11);
        map.put("C", 12);
        map.put("D", 13);
        map.put("E", 14);
        map.put("F", 15);
        // 获取待转换的十六进制的长度
        int length = str.length();// 输入的长度
        // 将待转换的十六进制转换成数组
        char[] cs = str.toCharArray();
        // result：用于存结果
        int result = 0;
        // 遍历数组
        for (int i = 0; i < str.length(); i++) {
            //取出每一个字符
            char x = cs[i];
            if (x >= 65 && x <= 70) {//若为A--F
                int r = map.get("" + x);
                result += r * Math.pow(16, length - 1);
            } else {
                result += (x - '0') * Math.pow(16, length - 1);
            }
            length--;
        }
        return result;
    }

    public static String DtoH1(int x) {
        // 存放十六进制的十六个数
        String[] str = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
        // 临时存放所有余数
        LinkedList<Integer> linkedList = new LinkedList<Integer>();
        // m:临时存放每次计算的余数
        int m = 0;
        while (x != 0) {
            m = x % 16;// m存余数
            linkedList.push(m);
            x = x / 16;
        }
        // reuslt：存放结果
        String reuslt = "";
        while (linkedList.size() != 0) {
            System.out.println("list : " + linkedList);
            Integer popInteger = linkedList.pop();
            reuslt = reuslt + str[popInteger];
        }
        System.out.println(reuslt);
        return reuslt;
    }


    public static void main(String[] args) {
//        int vaueTen = 1473;
//        BigInteger vaueTen = new BigInteger("1470010021");
        BigInteger vaueTen = new BigInteger("8999999990");
        int vaueTen1 = 703820;
//        String value16 = "57-D6-EF-8C";
        DtoH(vaueTen);
//        DtoH(vaueTen1);
//        HtoD(value16);
    }
}
